package com.hisense.ovcloud.commons.tools;

import cn.hutool.core.codec.Base64;
import lombok.extern.slf4j.Slf4j;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;

@Slf4j
public class AES256CbcUtil {

    public static void main(String[] args) {
//        String plainTxt = "5e5fe53110eee4c2b6d14843f93e2142";
        String key = "7f0b9bd6859821632f2413b9ed033c33";
//        String iv = key.substring(0, 16);
//                plainTxt);
//        System.out.println("encrtptedTxt: " + encrtptedTxt);
        String a = "aA0IIRDc4pVG2DivOL3bRRAFI+sEAmAJK4lsN1sfQrdDxgoS4eHs03xgykOVtWJd";
//        String deBase64 = new String(Base64.decode(a.getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8);
//        System.out.println("debase64: " + deBase64);
        System.out.println("plainTxt: " + decrypt256CBCWithoutIv("7f0b9bd6859821632f2413b9ed033c33", a));
    }

    public static String decrypt256CBC(String cryptText, String key, String iv) {
        try {
            SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");
            IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes(StandardCharsets.UTF_8));
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivSpec);
            byte[] s = Base64.decode(cryptText.getBytes(StandardCharsets.UTF_8));
            return new String(cipher.doFinal(s));
        } catch (Exception e) {
            log.error(e.getMessage());
            return null;
        }
    }

    public static String encrypt256CBC(String plainText, String key, String iv) {
        try {
            SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");
            IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes(StandardCharsets.UTF_8));
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivSpec);
            byte[] original = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));
            return Base64.encode(original);
        } catch (Exception e) {
            log.error(e.getMessage());
            return null;
        }
    }

    public static String encrypt256CBCWithoutIv(String key, byte[] plainText) {
        try {
            SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");
            IvParameterSpec ivSpec = new IvParameterSpec(key.substring(0, 16).getBytes(StandardCharsets.UTF_8));
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivSpec);
            byte[] original = cipher.doFinal(plainText);
            return Base64.encode(original);
        } catch (Exception e) {
            log.error(e.getMessage());
            return "";
        }
    }


    public static String decrypt256CBCWithoutIv(String key, String cryptText) {
        try {
            SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");
            IvParameterSpec ivSpec = new IvParameterSpec(key.substring(0, 16).getBytes(StandardCharsets.UTF_8));
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivSpec);
            byte[] s = Base64.decode(cryptText.getBytes(StandardCharsets.UTF_8));
            return new String(cipher.doFinal(s));
        } catch (Exception e) {
            log.error(e.getMessage());
            return "";
        }
    }
}

